/*
StreetsDropdown By Jimmy
*/

if (typeof(Streets) == "undefined")
	Streets = { };

Streets.Dropdown = new Class({
	Implements:Options,

	options:{
		openDelay:100,
		closeDelay:200,
		duration:50,
		link:'cancel',
		transition:Fx.Transitions.Sine.easeOut,
		mode:'horizontal' //horizontal|vertical
	},

	initialize: function(menu,options){
		this.menu = menu;
		this.setOptions(options);
		if(Browser.Engine.webkit){
			window.addEvent('domready',this.domReady.delay(200,this));
		}else{
			window.addEvent('domready',this.domReady.bind(this));
		}
	},

	domReady:function(){
		this.menu = $(this.menu);
		if(!$defined(this.menu)){
			return false;
		}
		this.createSubmenu(this.menu);
		if(this.options.mode=='horizontal'){
			var clear = new Element('li',{
				html:"&nbsp;", 
				styles:{
					clear:('left'),
					display:(Browser.Engine.trident?'inline':'block'),
					position:'relative',
					top:0,
					height:0,
					width:0,
					fontSize:0,
					lineHeight:0,
					margin:0,
					padding:0
				}
			}).addClass("clear").inject(this.menu);

		}
	},
	attachLiEvent : function(li){
		if (li.hasClass('split')) return;
		var over = "over";
		if (li.hasClass('up')) over = 'up-over';
		if (li.hasClass('left')) over = 'left-over';
		li.addEvents({
			'mouseenter' : function(){
				li.addClass(over);				
				$clear(li.retrieve('closeDelay'));
				if (!li.getFirst('a').hasClass("disabled")){
					var innerUL = li.getFirst('ul');
					if ($defined(innerUL)){
						this.createSubmenu(innerUL);
						li.store('openDelay',this.showSubmenu.delay(this.options.openDelay,this,innerUL));
					}
					
				}
			}.bind(this),
			'mouseleave' : function(){
				li.removeClass(over);
				$clear(li.retrieve('openDelay'));
				if (!li.getFirst('a').hasClass("disabled")){
					var innerUL = li.getFirst('ul');
					if ($defined(innerUL) && innerUL.get('c_')){
						li.store('closeDelay',this.hideSubmenu.delay(this.options.closeDelay,this,innerUL));
					}
				}
			}.bind(this)
		});
	},
	refresh : function(li){
		var p = ($defined(li))?li.getParent():this.menu;
		if (!$defined(p)) p = this.menu;
		p.erase('c_');
		p.setStyle('width','auto');
		p.getChildren('li').setStyle('width','auto');
		this.createSubmenu(p);
	},
	createSubmenu:function(ul){
		if (!ul.get('c_')){
			if (ul != this.menu ){
				ul.setStyles({
					display:'block',
					visibility:'hidden',
					opacity:0
				});
			}
			var LIs = ul.getChildren('li');
			var w =0;var h=0;
			LIs.each(function(li){
				li.setStyle('position','relative');
				w = w > li.getSize().x ? w : li.getSize().x;
				h = h + li.getSize().y;
				this.attachLiEvent(li);
			},this);
					
			if (w>0){
				if (ul!=this.menu || this.options.mode=='vertical'){
					w=w+ul.getStyle('border-width').toInt()+ul.getStyle('border-width').toInt();
					ul.setStyles({
						width:w,
						Height:h
					});
					ul.getChildren('li').setStyle('width',w);
				}
			}
			ul.set('c_',1);
		}
		w = ul.getSize().x;
		h = ul.getSize().y;
		
		if (ul != this.menu ){
			var x,y;
			var pLI = ul.getParent();
			var left = pLI.getPosition().x;
			var top = pLI.getPosition().y;
			ul.erase('r_');
			if (pLI.hasClass('up')){
				x = 0;
				y = pLI.getSize().y;
				if(left+x+w >= $(document.body).getSize().x){
					if (w - pLI.getCoordinates().width< left){
						x = -w + pLI.getCoordinates().width;
						ul.set('r_',1);
					}
				}
				if(top + h >= $(document.body).getSize().y){
					y = top > h ? -h : -top;
				}
			}
			if (pLI.hasClass('left')){
				x = pLI.getSize().x;				
				y = -ul.getStyle('border-left-width').toInt();
				if((left+x+w >= $(document.body).getSize().x) || pLI.getParent().get('r_')){
					if (w < left){
						x = -w;
						ul.set('r_',1);
					}
				}
				if(top + h >= $(document.body).getSize().y){
					var overloap = top + h - $(document.body).getSize().y + 10;
					if (overloap < top){
						y = -overloap;
					}
				}
			}						
			
			ul.setStyles({				
				top:y,
				left : x
			});
			ul.store('animation',new Fx.Morph(ul,this.options));
		}
	},
	showSubmenu : function(ul){
		var animation = ul.retrieve('animation');
		animation.cancel();
		var animobject = {opacity:1};
		animation.start(animobject);
	},
	hideSubmenu : function(ul){
		var animation = ul.retrieve('animation');
		animation.cancel();		
		var animobject = {opacity:0};
		animation.start(animobject);
	}
});